//
//  AMPSADJGSplashAdapter.m
//  AdScopeDemo
//
//  Created by apple on 2025/5/22.
//

#import "AMPSADJGSplashAdapter.h"
#import <ADJgSDK/ADJgSDKSplashAd.h>
#import <AdScopeFoundation/AdScopeFoundation.h>
#import "AMPSADJGSDKManagerAdapter.h"

@interface AMPSADJGSplashAdapter () <ADJgSDKSplashAdDelegate>

@property (nonatomic, assign) BOOL isBiddingRequest;

@property (nonatomic, strong) ADJgSDKSplashAd *splashAd;

@property (nonatomic, strong) AMPSAdConfiguration *adConfiguration;

@property (nonatomic, strong) AdScopeTimer *timeoutTimer;

@end

@implementation AMPSADJGSplashAdapter

- (void)sendWinNotificationWithInfo:(NSDictionary *)winInfo {
   
}

- (void)sendLossNotificationWithInfo:(NSDictionary *)lossInfo {
   
}

- (void)requestBiddingPriceWithSpaceId:(NSString *)spaceId
                       adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    self.isBiddingRequest = YES;
    [self loadAdWithSpaceId:spaceId adConfiguration:adConfiguration];
}

- (void)loadAdWithSpaceId:(NSString *)spaceId adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    if (AMPSADJGSDKManagerAdapter.initState != kAdScopeMediationAdapterInitSDKStateSuccess) {
        __weak typeof(self)weakSelf = self;
        [AMPSADJGSDKManagerAdapter startAsyncWithAppId:self.appId configuration:self.sdkConfiguration results:^(BOOL adapterResult) {
            __strong typeof(weakSelf)strongSelf = weakSelf;
            if (adapterResult) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [strongSelf gdtLoadAdWithSpaceId:spaceId adConfiguration:adConfiguration];
                });
            } else {
                if (strongSelf.isBiddingRequest) {
                    NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:250401 userInfo:@{@"NSLocalizedDescription": @"AMPSADJGSDKManagerAdapter init fail"}];
                    if (strongSelf.bidDelegate && [strongSelf.bidDelegate respondsToSelector:@selector(requestBiddingPriceFail:error:ext:)]) {
                        [strongSelf.bidDelegate requestBiddingPriceFail:strongSelf error:error ext:adConfiguration.ext];
                    }
                } else {
                    NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:270401 userInfo:@{@"NSLocalizedDescription": @"AMPSADJGSDKManagerAdapter init fail"}];
                    if (strongSelf.customDelegate && [strongSelf.customDelegate respondsToSelector:@selector(splashAdLoadFail:error:ext:)]) {
                        [strongSelf.customDelegate splashAdLoadFail:strongSelf error:error ext:adConfiguration.ext];
                    }
                }
            }
        }];
    } else {
        [self gdtLoadAdWithSpaceId:spaceId adConfiguration:adConfiguration];
    }
}

- (void)gdtLoadAdWithSpaceId:(NSString *)spaceId adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    self.splashAd = [[ADJgSDKSplashAd alloc] init];
    self.splashAd.controller = adConfiguration.viewController;
    self.splashAd.posId = spaceId;
    self.adConfiguration = adConfiguration;
    self.splashAd.delegate = self;
    CGFloat floatValue = adConfiguration.timeoutInterval/1000 > 0 ? adConfiguration.timeoutInterval/1000 : 5;
    self.timeoutTimer = [AdScopeTimer adScopeScheduledWeakTimerWithTimeInterval:floatValue target:self selector:@selector(timeoutTimerSelector) repeats:NO];
    [self.splashAd loadAdInWindow:[UIApplication sharedApplication].keyWindow withBottomView:adConfiguration.bottomView];
}

- (void)timeoutTimerSelector {
//    NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:-9999 userInfo:@{@"NSLocalizedDescription": @"AMPSADJGSplashAdapter load ad timeout"}];
    [self adjg_splashAdFailToPresentScreen:self.splashAd failToPresentScreen:[ADJgAdapterErrorDefine new]];
}

- (void)loadC2SAdWithSpaceId:(NSString *)spaceId
                biddingToken:(id)biddingToken
             adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    if (self.isLoadAdSuccess) {
        if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(splashAdLoadSuccess:ext:)]) {
            [self.customDelegate splashAdLoadSuccess:self ext:self.adConfiguration.ext];
        }
    }
    
}

- (void)loadS2SAdWithSpaceId:(NSString *)spaceId
                biddingToken:(id)biddingToken
             adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    
}

- (NSInteger)eCPM {
    ADJgSDKExtInfo *extInfo = [self.splashAd adjg_extInfo];
    return [extInfo.ecpm integerValue];
}

- (BOOL)isValid {
    return self.isLoadAdSuccess;
}

- (void)showSplashViewInWindow:(nonnull UIWindow *)window {
    [self.splashAd showAdInWindow:window];
}

- (void)adjg_splashAdSuccessToLoadAd:(ADJgSDKSplashAd *)splashAd {
    if (_timeoutTimer) {
        [_timeoutTimer invalidate];
        _timeoutTimer = nil;
    }
    self.isLoadAdSuccess = YES;
    if (!self.isBiddingRequest) {
        if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(splashAdLoadSuccess:ext:)]) {
            [self.customDelegate splashAdLoadSuccess:self ext:self.adConfiguration.ext];
        }
        return;
    }
    ADJgSDKExtInfo *extInfo = [self.splashAd adjg_extInfo];
    if ([extInfo.ecpm integerValue] > 0) {
        if (self.bidDelegate && [self.bidDelegate respondsToSelector:@selector(requestBiddingPriceSuccess:ext:)]) {
            [self.bidDelegate requestBiddingPriceSuccess:self ext:self.adConfiguration.ext];
        }
    } else {
        if (self.bidDelegate && [self.bidDelegate respondsToSelector:@selector(requestBiddingPriceFail:error:ext:)]) {
            NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:250401 userInfo:@{@"NSLocalizedDescription": @"AMPSADJGSplashAdapter ad bidding fail,ecpm less than 0"}];
            [self.bidDelegate requestBiddingPriceFail:self error:error ext:self.adConfiguration.ext];
        }
    }
}

- (void)adjg_splashAdSuccessToPresentScreen:(ADJgSDKSplashAd *)splashAd {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(splashAdDidShow:)]) {
        [self.customDelegate splashAdDidShow:self];
    }
}

- (void)adjg_splashAdEffective:(ADJgSDKSplashAd *)splashAd {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(splashAdExposured:)]) {
        [self.customDelegate splashAdExposured:self];
    }
}

- (void)adjg_splashAdClicked:(ADJgSDKSplashAd *)splashAd {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(splashAdDidClick:ext:)]) {
        [self.customDelegate splashAdDidClick:self ext:self.adConfiguration.ext];
    }
}

- (void)adjg_splashAdClosed:(ADJgSDKSplashAd *)splashAd {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(splashAdDidClose:)]) {
        [self.customDelegate splashAdDidClose:self];
    }
    [self removeAd];
}

- (void)adjg_splashAdFailToPresentScreen:(ADJgSDKSplashAd *)splashAd failToPresentScreen:(ADJgAdapterErrorDefine *)error {
    NSError *tempError = ADJgAdapterError(ADJgAdapterErrorDomain, error.code, error.message);
    if (self.isBiddingRequest) {
        if (_isLoadAdSuccess) {
            if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(splashAdShowFail:error:ext:)]) {
                [self.customDelegate splashAdShowFail:self error:tempError ext:self.adConfiguration.ext];
            }
        } else {
            if (self.bidDelegate && [self.bidDelegate respondsToSelector:@selector(requestBiddingPriceFail:error:ext:)]) {
                [self.bidDelegate requestBiddingPriceFail:self error:tempError ext:self.adConfiguration.ext];
            }
        }
    } else {
        if (_isLoadAdSuccess) {
            if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(splashAdShowFail:error:ext:)]) {
                [self.customDelegate splashAdShowFail:self error:tempError ext:self.adConfiguration.ext];
            }
        } else {
            if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(splashAdLoadFail:error:ext:)]) {
                [self.customDelegate splashAdLoadFail:self error:tempError ext:self.adConfiguration.ext];
            }
        }
    }
    [self removeAd];
}

- (void)removeAd {
    if (_timeoutTimer) {
        [_timeoutTimer invalidate];
        _timeoutTimer = nil;
    }
    if (_splashAd) {
        _splashAd.delegate = nil;
        _splashAd = nil;
    }
}

- (void)dealloc {
    [self removeAd];
}

@end
